import pgzrun
import random as r
import sys
from tkinter import messagebox
import os

WIDTH = 801
HEIGHT = 639
TITLE = "纳西妲的花车"

lingche = Actor("lingche2.png",(400,550)) # type: ignore
all_huapin = []
all_flower = []
all_pailie = []
for i in range(3):
    for j in range(3):
        for n in range(3):
            if i == 2 and j == 2 and n == 2:
                break
            else:
                all_pailie.append([i,j,n])

# print(all_pailie)

counter = 0
counter_update_time = 25
speed = 75 / counter_update_time
live = 4
max_live = 4
score = 0

paint = False
debug = False

argv = sys.argv
argc = len(argv)

if "-wd" in argv:
    live = 114514
    max_live = 114514
if "-np" in argv:
    paint = False
if "-debug" in argv:
    debug = True

def health():
    a = r.choice([1,2,3])
    if a == 1:
        sounds.health1.play() #type:ignore
    elif a == 2:
        sounds.health2.play() #type:ignore
    elif a == 3:
        sounds.health3.play() #type:ignore
def hurt(is_died):
    if is_died:
        a = r.choice([1,2,3])
        if a == 1:
            sounds.died1.play() #type:ignore
        elif a == 2:
            sounds.died2.play() #type:ignore
        else:
            sounds.died3.play() #type:ignore
    else:
        sounds.hurt.play() #type:ignore
def update_speed():
    global counter_update_time,speed
    speed = 75 / counter_update_time
def spawn_huapin(a):
    global all_huapin
    #a = r.choice(range(5))
    if a == 0:
        all_huapin.append(Actor("huapin",(300,0))) # type: ignore
    elif a == 1:
        all_huapin.append(Actor("huapin",(400,0))) # type: ignore
    else:
        all_huapin.append(Actor("huapin",(500,0))) # type: ignore
def spawn_flower(a):
    global all_flower
    #a = r.choice(range(4))
    if a == 0:
        all_flower.append(Actor("flower",(300,0))) # type: ignore
    elif a == 1:
        all_flower.append(Actor("flower",(400,0))) # type: ignore
    else:
        all_flower.append(Actor("flower",(500,0))) # type: ignore
def spawn_one(thing,where):
    global debug
    if thing == 1:
        if r.choice(range(5)) == 0:
            if debug:
                print("spawned " + "flower" + " " + str(where))
            spawn_flower(where)
    elif thing == 2:
        if r.choice(range(2)) == 0:
            if debug:
                print("spawned " + "huapin" + " " + str(where))
            spawn_huapin(where)
    else:
        pass
def spawn():
    global debug
    a = r.choice(all_pailie)
    for i in range(len(a)):
        if debug:
            print("spawned " + str(a[i]) + " " + str(i))
        spawn_one(a[i],i)
def zuoyouqiehuan(fangxiang):
    if fangxiang == 0:
        if lingche.x == 300:
            pass
        else:
            lingche.x -= 100
    elif fangxiang == 1:
        if lingche.x == 500:
            pass
        else:
            lingche.x += 100
    else:
        pass
def update():
    global debug
    global all_flower
    global all_huapin
    global counter,counter_update_time,speed,lingche
    global live,score,max_live,paint
    counter += 1
    #print(counter)
    if counter % counter_update_time == 0:
        spawn()
        #counter = 0
        if debug:
            print("==========")
    for i in all_flower:
        i.y += speed
        if i.top >= 639:
            i.image = "none"
        if i.colliderect(lingche):
            i.image = "none"
            i.top = lingche.bottom + 10
            if live < max_live:
                live += 1
            else:
                score += 5
            health()
            if paint:
                print("纳西妲表示：真香，现在纳西妲还剩" + str(live) + "点血，还剩" + str(score) + "点分数")
    for i in all_huapin:
        i.y += speed
        if i.top >= 639:
            i.image = "none"
        if i.colliderect(lingche):
            i.image = "none"
            i.top = lingche.bottom + 10
            live -= 1
            hurt(False)
            if paint:
                print("纳西妲表示：阿米诺斯，现在纳西妲还剩" + str(live) + "点血，还剩" + str(score) + "点分数")
    if live == 0:
        if max_live == 0:
            messagebox.showinfo("游戏结束","纳西妲因为花车开的还没她跑得快愤怒地下了车，你失败了！")
            exit()
        else:
            hurt(True)
            messagebox.showinfo("游戏结束","纳西妲蛋黄被摇匀了，你失败了！")
            exit()
        
def on_mouse_up(pos,button):
    global counter_update_time,max_live,paint,live
    if button == 1:
        zuoyouqiehuan(0)
    elif button == 2:
        if paint == True:
            paint = False
            print("关闭输出")
        else:
            paint = True
            print("开启输出")
    elif button == 3:
        zuoyouqiehuan(1)
    elif button == 4:
        if counter_update_time - 5 <= 0:
            if paint:
                print("纳西妲表示：阿米诺斯，再加快我要被颠飞了")
        else:
            max_live += 1
            counter_update_time -= 5
            update_speed()
    elif button == 5:
        counter_update_time += 5
        update_speed()
        max_live -= 1
        if live > max_live:
            live = max_live

def draw():
    global live,score,max_live
    screen.blit("background.png",(0,0)) # type: ignore
    lingche.draw()
    for i in all_flower:
        i.draw()
    for i in all_huapin:
        i.draw()
    screen.draw.text(str(live) + "/" + str(max_live),(10,10),color='black',fontname='main') # type: ignore
    screen.draw.text(str(score),(450,10),color='black',fontname='main') # type: ignore

music.play("main.mp3") # type: ignore
pgzrun.go()
if debug:
    os.system("pause")